package com.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

	private static final int DEFAULT_EXPIRE_SECONDS = 3600 * 24 * 10; // 默认cookie有效期: 10天

	private static Map<String, String> getCookieValueMap(HttpServletRequest request) {
		Map<String, String> cookieMap = new HashMap<>();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie.getValue());
			}
		}
		return cookieMap;
	}

	/**获取cookie
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getCookieValue(HttpServletRequest request, String name) {
		Map<String, String> cookieValueMap = getCookieValueMap(request);
		return cookieValueMap.get(name);
	}

	/**获取cookie
	 * @param name
	 * @return
	 */
	public static String getCookieValue(String name) {
		Map<String, String> cookieValueMap = getCookieValueMap(RequestUtil.getRequest());
		return cookieValueMap.get(name);
	}

	/**设置cookie，有效期默认10天
	 * @param name
	 * @param value
	 */
	public static void setCookie(String name, String value) {
		setCookie(RequestUtil.getRequest(), RequestUtil.getResponse(), name, value);
	}

	/**设置cookie，有效期默认10天
	 * @param request
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
		setCookie(request, response, name, value, null);
    }

	/**设置cookie，在有效期内有效
	 * @param name
	 * @param value
	 * @param maxAgeSecond
	 */
	public static void setCookie(String name, String value, Integer maxAgeSecond) {
		setCookie(RequestUtil.getRequest(), RequestUtil.getResponse(), name, value, maxAgeSecond);
	}

	/**设置cookie，在有效期内有效
	 * @param request
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAgeSecond
	 */
	public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, Integer maxAgeSecond) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		cookie.setDomain(request.getServerName());
		cookie.setMaxAge(maxAgeSecond == null ? DEFAULT_EXPIRE_SECONDS : maxAgeSecond);
	    response.addCookie(cookie);
    }

    /**删除cookie
     * @param request
     * @param response
     * @param name
     */
    public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie cookie = new Cookie(name, null);
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    /**删除cookie
     * @param name
     */
    public static void deleteCookie(String name) {
    	Cookie cookie = new Cookie(name, null);
    	cookie.setPath(RequestUtil.getRequest().getContextPath());
    	cookie.setMaxAge(0);
    	RequestUtil.getResponse().addCookie(cookie);
    }

}
